﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml;
using JimBlackler.DocsByReflection;

namespace LifeFlow.Servicer
{
    [AttributeUsage(AttributeTargets.Method, Inherited = true, AllowMultiple = true)]
    public class ServiceAttribute : Attribute
    {
        public string Service { get; set; }
        public string Action { get; set; }
        public ServiceVersion MinServiceVersion { get; set; }
        public ServiceVersion MaxServiceVersion { get; set; }
        public bool RequireKey { get; set; }

        public string MinVersion
        {
            get { return MinServiceVersion.ToString(); }
            set
            {
                MinServiceVersion = new ServiceVersion(value);
            }
        }

        public string MaxVersion
        {
            get { return MaxServiceVersion.ToString(); }
            set
            {
                MaxServiceVersion = new ServiceVersion(value);
            }
        }

        public ServiceDescription GetDescription(MethodInfo type)
        {
            var result = new ServiceDescription()
                             {
                                 Service = Service,
                                 Action = Action
                             };

            var returnType = new ServiceDescription.ServiceParameter()
                                 {
                                     Name = type.ReturnType.Name
                                 };
            result.Return = returnType;
            var parameters = new List<ServiceDescription.ServiceParameter>();
            try
            {
                var documentation = DocsByReflection.XMLFromMember(type);
                if (documentation["param"] != null)
                    foreach (XmlNode node in documentation["param"].ChildNodes)
                    {
                        var p = new ServiceDescription.ServiceParameter();
                        try
                        { p.Name = node.ParentNode.Attributes["name"].InnerText; }
                        catch (Exception)
                        { }
                        try
                        { p.Type = node.ParentNode.Attributes["type"].InnerText; }
                        catch (Exception)
                        { }
                        try
                        { p.Description = node.InnerText.Trim(); }
                        catch (Exception)
                        { }
                        try
                        { p.Optional = node.ParentNode.Attributes["optional"].InnerText; }
                        catch (Exception)
                        { }
                        parameters.Add(p);
                    }
            }
            catch (Exception)
            {
            }



            result.Parameters = parameters.ToArray();

            try
            {
                var documentation = DocsByReflection.XMLFromMember(type);

                if (documentation["summary"] != null)
                    result.Summery = documentation["summary"].InnerText.Trim();

                if (documentation["returns"] != null)
                    result.Return.Description = documentation["returns"].InnerText.Trim();
            }
            catch (Exception)
            {
            }
            return result;
        }
    }
}
